package net.interest.curation;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.logging.Logger;

import net.interest.curation.meta.RssMeta;
import net.interest.curation.meta.ScrapBookMeta;
import net.interest.curation.model.Rss;
import net.interest.curation.model.ScrapBook;
import net.interest.curation.model.User;

import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import scenic3.ScenicPage;
import scenic3.annotation.ActionPath;
import scenic3.annotation.Page;

import com.google.gson.stream.JsonWriter;


@Page("/rss")
public class RssPage  extends ScenicPage {
    private static final Logger log =
            Logger.getLogger(RssPage.class.getName());

    @ActionPath("add")
    public Navigation add() {
        User user = (User) super.request.getSession().getAttribute(User.SESSION_USER_ATTR_NAME);

        String title = request.getParameter("title");
        String url = request.getParameter("url");
        
        Rss rss = new Rss();
        rss.getUserRef().setModel(user);
        rss.setTitle(title);
        rss.setUrl(url);
        Datastore.put(rss);
        
        
        super.response.setContentType("text/json");
        JsonWriter writer;
        try {
            writer = new JsonWriter(new OutputStreamWriter(super.response.getOutputStream(), "UTF-8"));
            writer.beginObject();
            writer.name("key");
            writer.value(rss.getKey().getId());
            writer.name("title");
            writer.value(title);
            writer.endObject();
            writer.flush();
            writer.close();
        } catch (UnsupportedEncodingException e) {
            log.severe(e.getLocalizedMessage());
        } catch (IOException e) {
            log.severe(e.getLocalizedMessage());
       }
        return null;
    
    }

    @ActionPath("list")
    public Navigation list() {
        User user = (User) super.request.getSession().getAttribute(User.SESSION_USER_ATTR_NAME);
        
        RssMeta meta = RssMeta.get();
        List<Rss> rsses = Datastore.query(meta).filter(meta.userRef.equal(user.getKey())).asList();

        super.response.setContentType("text/json");
        JsonWriter writer;
        try {
            writer = new JsonWriter(new OutputStreamWriter(super.response.getOutputStream(), "UTF-8"));
            writer.beginArray();
            for(Rss rss : rsses){
                writer.beginObject();
                writer.name("key");
                writer.value(rss.getKey().getId());
                writer.name("title");
                writer.value(rss.getTitle());
                writer.name("url");
                writer.value(rss.getUrl());
                writer.endObject();
            }
            writer.endArray();
            writer.flush();
            writer.close();
        } catch (UnsupportedEncodingException e) {
            log.severe(e.getLocalizedMessage());
        } catch (IOException e) {
            log.severe(e.getLocalizedMessage());
       }
        return null;
    }

}
